package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.dto.ResultAllService;
import com.woniuxy.dto.ResultFinishedSercer;
import com.woniuxy.dto.ResultHasServer;
import com.woniuxy.mq.DirectMessageSender;
import com.woniuxy.pojo.ServerOrders;
import com.woniuxy.service.ProServerService;
import com.woniuxy.service.ServerOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/venser")
@CrossOrigin(allowCredentials="true",maxAge = 3600)
@Api(tags = "物业处理服务类")
public class ProServerController {

    @Autowired
    private ProServerService proServerService;

    @Autowired
    private ServerOrderService serverOrderService;

    @Autowired
    DirectMessageSender sender;

    //物业服务界面，展示所有可接取的服务（用户预约物业的服务）
    @GetMapping("/allServer")
    @ResponseBody
    @ApiOperation(value = "物业服务界面，展示所有可接取的服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vid",value = "物业表id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "page",value = "页码",required = false,dataType = "Integer"),
            @ApiImplicitParam(name = "limit",value = "每页数量",required = false,dataType = "Integer"),
    })
    public Map showAllServer(Long vid,String name,
                             @RequestParam(defaultValue = "1",required = false,name = "page") Integer page,
                             @RequestParam(defaultValue = "10",required = false,name = "limit") Integer limit){
        System.out.println(vid+"ssssssssss"+page+"///"+limit+"//"+name);
        Map<String,Object> map = new HashMap<>();
        List<ResultAllService> lists = proServerService.findAllService(vid,name,page,limit);
        PageInfo info = new PageInfo(lists);
        map.put("code",0);
        map.put("msg","");
        map.put("count",info.getTotal());
        map.put("data",lists);
        return map;
    }

    //物业接取用户服务
    //参数ssid为传入的预约表id
    @PostMapping("/receiveServer")
    @ResponseBody
    @ApiOperation(value = "物业接取用户服务")
    @ApiImplicitParam(name = "ssid",value = "预约表id",required = true,dataType = "Long")
    public Map receiveServer(Long ssid){
        Map map = new HashMap();
        try {
            proServerService.receiveServer(ssid);
            sender.send("物业提示:您预约的服务已被接取，请按预约时间等候。"+ssid);
            map.put("code",200);
            map.put("msg","服务接取成功！");
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","服务接取失败！");
        }
        return map;
    }


    //查询物业已接取的服务
    @GetMapping("/hasServer")
    @ResponseBody
    @ApiOperation(value = "查询物业已接取的服务")
    @ApiImplicitParam(name = "pid",value = "物业表id",required = true,dataType = "Long")
    public Map searchHasServer(Long pid){
        Map<String,Object> map = new HashMap<String,Object>();
        List<ResultHasServer> lists = proServerService.findHasServerByPropertyId(pid);
        map.put("code",0);
        map.put("msg","");
        map.put("count",null);
        map.put("data",lists);
        return map;
    }


    //物业点击确认完成后  soid为订单编号id
    @PostMapping("/checkOrder")
    @ResponseBody
    @ApiOperation(value = "物业点击确认完成后（服务完成）")
    @ApiImplicitParam(name = "soid",value = "订单表id",required = true,dataType = "Long")
    public Map checkOrderAccomplish(Long soid){
        Map<String,Object> map = new HashMap<>();
        ServerOrders so =serverOrderService.checkOrderAccomplish(soid);
        if(so == null){
            map.put("code",500);
            map.put("msg","该订单不存在,请确认后重试！");
        }else {
            //获取当前时间，如果当前时间小于预约时间则不允许提交（还未服务）
            Date date = new Date();
            if (date.before(so.getSstime())){
                map.put("code",500);
                map.put("msg","当前时间小于预约时间，不允许提交！");
            }else if(so.getEndTime() == null){
                //当前时间大于预约时间，可以确认完成
                //System.out.println("可以完成！");
                //通过rabbitMq将消息发送给用户，等候用户确认
                //sendMessage("物业提示:您的服务已完成,请确认后点击完成！");
                sender.send("物业提示:您预约的服务已完成,请在个人中心中确认,订单号为:"+so.getOrderNum());
                //将服务结束时间插入数据库
                serverOrderService.setEndTimeById(soid,new Date());
                map.put("code",200);
                map.put("msg","确认完成中，请等候用户确认！");
            }else {
                map.put("code",200);
                map.put("msg","此订单已完成,等候用户确认,如24小时用户未确认，则默认完成！");
            }
        }
        return map;
    }



    //物业显示已完成的订单
    @GetMapping("/finished")
    @ResponseBody
    @ApiOperation(value = "物业显示已完成的订单")
    @ApiImplicitParam(name = "pid",value = "物业表id",required = true,dataType = "Long")
    public Map finishedServerOrder(Long pid){
        Map<String,Object> map = new HashMap<>();
        List<ResultFinishedSercer>  lists = proServerService.findFinishServerOrder(pid);
        map.put("code",0);
        map.put("msg","");
        map.put("count",null);
        map.put("data",lists);
        return map;
    }





}
